package servlet;

import bean.User;
import dao.UserDao;

import javax.servlet.*;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.File;
import java.io.IOException;
import java.util.List;

@WebServlet("/userServlet")
@MultipartConfig
public class UserServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public UserServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.setCharacterEncoding("UTF-8");
//        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

        if ("add".equals(request.getParameter("add")) ) {
            String usernameu = request.getParameter("usernameu");
            String passwordu = request.getParameter("passwordu");
            String sex = request.getParameter("sex");
            String role = request.getParameter("role");
            Part filePart = request.getPart("avataru");

            if (filePart != null && filePart.getSize() > 0) {
                String fileName = getSubmittedFileName(filePart);
                String uploadPath = "C:/程序设计/JavaWeb/press-release-system/web/resources/headimg";
                File uploadDir = new File(uploadPath);
                if (!uploadDir.exists()) {
                    uploadDir.mkdirs();
                }
                String filePath = uploadPath + File.separator + fileName;
                filePart.write(filePath);

                UserDao.Insert(usernameu, passwordu, sex, role, fileName);
            } else {
                UserDao.Insert(usernameu, passwordu, sex, role, null);
            }

            List<User> userlist =  UserDao.select();
            request.getSession().setAttribute("userlist", userlist);
            response.sendRedirect("/press_release_system_war_exploded/userlimit"); // Redirect to user limit page
        }

        if ("update1".equals(request.getParameter("update1"))) {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String sex = request.getParameter("sex");
//            User user1 = (User) request.getSession().getAttribute("user");
//            String name = user1.getUsername();
//            String headimg = user1.getHeadimg();
            String id = request.getParameter("id");
            Part filePart = request.getPart("avatar");
            if (filePart != null && filePart.getSize() > 0) {
                String fileName = getSubmittedFileName(filePart);
                String uploadPath = "C:/程序设计/JavaWeb/press-release-system/web/resources/headimg";
                File uploadDir = new File(uploadPath);
                if (!uploadDir.exists()) {
                    uploadDir.mkdirs();
                }
                String filePath = uploadPath + File.separator + fileName;
                filePart.write(filePath);

                UserDao.update(username, password, sex, fileName, id);
//                user1.setHeadimg(fileName);
            } else {
                UserDao.update(username, password, sex, null, id);
            }

//            user1.setUsername(username);
//            user1.setPassword(password);
//            user1.setSex(sex);
//            request.getSession().setAttribute("user", user1);
//            request.getSession().setAttribute("updateSuccess", true);

            response.sendRedirect("./jsp/login.jsp"); // Redirect to login page
        }

        if ("update2".equals(request.getParameter("update2"))) {
            String usernameuu = request.getParameter("usernameuu");
            String passworduu = request.getParameter("passworduu");
            String sex = request.getParameter("sex");
            String role = request.getParameter("role");
            String id = request.getParameter("id");

            Part filePart = request.getPart("avataruu");
            if (filePart != null && filePart.getSize() > 0) {
                String fileName = getSubmittedFileName(filePart);
                String uploadPath = "C:/程序设计/JavaWeb/press-release-system/web/resources/headimg";
                File uploadDir = new File(uploadPath);
                if (!uploadDir.exists()) {
                    uploadDir.mkdirs();
                }
                String filePath = uploadPath + File.separator + fileName;
                filePart.write(filePath);

                UserDao.update(usernameuu, passworduu, sex, role, fileName, id);
            } else {
                UserDao.update(usernameuu, passworduu, sex, role, null, id);
            }

            List<User> userlist =  UserDao.select();
            request.getSession().setAttribute("userlist", userlist);
            response.sendRedirect("/press_release_system_war_exploded/userlimit"); // Redirect to user limit page
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    private String getSubmittedFileName(Part part) {
        for (String cd : part.getHeader("content-disposition").split(";")) {
            if (cd.trim().startsWith("filename")) {
                String fileName = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
                return fileName.substring(fileName.lastIndexOf('/') + 1).substring(fileName.lastIndexOf('\\') + 1);
            }
        }
        return null;
    }
}
